package com.nix.fileresolution.excel.read.readPojo;

import com.nix.fileresolution.Read;
import com.nix.fileresolution.excel.read.XRow;
import com.nix.web.databases.pojo.word.Category;
import com.nix.web.databases.pojo.word.Word;
import com.nix.web.databases.service.iteratory.Iterator;
import com.nix.web.databases.service.iteratory.ObjectAggregate;

import java.util.ArrayList;


/**
 * 专门为封装word创建的类
 * Create by zhangpe0312@qq.com on 2018/1/3.
 */
public class ReadWord implements ReadPojo {

    //它们为在excel中的列号
    private int name;
    private int pinYin;
    private int wordMeaning;
    private int frequency;
    private int category;

    /**
     * 封装word的类
     * @param name 单词名字在哪列
     * @param pinYin 单词拼音列
     * @param wordMeaning 单词意思列
     * @param frequency 单词频率列
     * @param category 单词词性列
     */
    public ReadWord(int name, int pinYin, int wordMeaning, int frequency, int category) {
        this.name = name;
        this.pinYin = pinYin;
        this.wordMeaning = wordMeaning;
        this.frequency = frequency;
        this.category = category;
    }
    @Override
    public Iterator read(Read<? extends Read> read) {
        try {
            ArrayList<XRow> rows = (ArrayList<XRow>) read.read();
            ObjectAggregate<Word> wordAggregate = new ObjectAggregate<Word>();
            for (XRow row :
                    rows) {
                Word word = new Word();
                word.setName(row.getCell(name).getValue());
                word.setFrequency(Integer.parseInt(row.getCell(frequency).getValue()));
                word.setPinYin(row.getCell(pinYin).getValue());
                Category category = new Category();
                category.setMark(row.getCell(this.category).getValue());
                word.setCategory(category);
                wordAggregate.append(word);
            }
            System.out.println("行数据"+rows.size() + " aggregate : " + wordAggregate.getList().size());
            Iterator<Word> iterator = wordAggregate.iterator();
            return iterator;
        } catch (Exception e) {
            System.out.println("excel - > 单词组装失败 " + e);
        }
        return null;
    }


    public int getName() {
        return name;
    }

    public void setName(int name) {
        this.name = name;
    }

    public int getPinYin() {
        return pinYin;
    }

    public void setPinYin(int pinYin) {
        this.pinYin = pinYin;
    }

    public int getWordMeaning() {
        return wordMeaning;
    }

    public void setWordMeaning(int wordMeaning) {
        this.wordMeaning = wordMeaning;
    }

    public int getFrequency() {
        return frequency;
    }

    public void setFrequency(int frequency) {
        this.frequency = frequency;
    }

    public int getCategory() {
        return category;
    }

    public void setCategory(int category) {
        this.category = category;
    }
}
